var wineApp = wineApp || {};
wineApp.modaddress = (function() {
    var resourceURL = wineApp.tool.getResourceURL();
    var addrId = $.url().param()["addrId"];
    var referURL = $.url().param()["referUrl"]
    console.log($.url().param())
    if (addrId == null) addrId = "";
    var addressObj = {
            recname: '',
            phone: '',
            province: '',
            city: '',
            town: '',
            address: '',
            postcode: '',
            defaultflag: 0
        }
        //var ajaxUrl = wineApp.CONFIG.API.addressadd
    var initPage = function() {
        loadAddr();
        initEvent();
        //loadProvince();
    }
    var loadAddr = function() {
        if (addrId == '') {
            loadProvince();
            return;
        }
        var ajaxUrl = wineApp.CONFIG.API.addressbyid;
        wineApp.ajax.post(ajaxUrl, {
            id: addrId
        }).done(function(data) {
            console.log(data);
            $.each(data.address, function(index, val) {
                var input = $('input[name=' + index + ']');
                input.val(val);
            });
            if (data.address.defaultflag == 1) {
                $('#defaultflag').prop('checked', true);
            }
            loadProvince();
            $('#province').val(data.provid);
            loadCity(data.provid, data.cityid);
            loadTown(data.cityid, data.townid);

        })
    }
    var loadProvince = function() {
        var ajaxUrl = wineApp.CONFIG.API.provinces;
        wineApp.ajax.post(ajaxUrl, { async: false }).done(function(data) {
            $('#province').html('');
            $.each(data.list, function(index, val) {
                var option = $("<option>");
                option.attr('value', val.id);
                option.html(val.name)
                $('#province').append(option);
            });
        });
    }
    var loadCity = function(provid, cityid) {
        var ajaxUrl = wineApp.CONFIG.API.citys;
        wineApp.ajax.post(ajaxUrl, {
            async: false,
            provid: provid
        }).done(function(data) {
            $('#city').html('');
            $('#town').html('');
            emptyOption('city');
            emptyOption('town');
            $.each(data.list, function(index, val) {
                var option = $("<option>");
                option.attr('value', val.id);
                option.html(val.name)
                $('#city').append(option);
            });
            if (cityid) {
                $('#city').val(cityid);
            }
        });
    }
    var emptyOption = function(comp) {
        var option = $("<option>");
        option.attr('value', '');
        option.html('请选择');
        $('#' + comp).append(option);
    }
    var loadTown = function(cityid, townid) {
        var ajaxUrl = wineApp.CONFIG.API.towns;
        wineApp.ajax.post(ajaxUrl, {
            async: false,
            cityid: cityid
        }).done(function(data) {
            $('#town').html('');
            emptyOption('town');
            $.each(data.list, function(index, val) {
                var option = $("<option>");
                option.attr('value', val.id);
                option.html(val.name)
                $('#town').append(option);
            });
            if (townid) {
                $('#town').val(townid);
            }
        });
    }
    var initEvent = function() {
        $(document).on('click', '#save', function(e) {
            $.each(addressObj, function(index, val) {
                var input = $('input[name=' + index + ']');
                if (input.length > 0) {
                    addressObj[index] = input.val();
                }
            });

            var ajaxUrl = wineApp.CONFIG.API.addressadd;
            addressObj['address'] = $('input[name=address]').val();
            if (addrId != '') {
                addressObj['id'] = addrId;
                ajaxUrl = wineApp.CONFIG.API.addressupdate
            }
            addressObj['province'] = $("#province option:selected").text();
            addressObj['city'] = $("#city option:selected").text();
            addressObj['town'] = $("#town option:selected").text();
            var isDefault = document.getElementById('defaultflag').checked;
            if (isDefault) {
                addressObj.defaultflag = "1";
            } else {
                addressObj.defaultflag = "0";
            }
            if (_.isEmpty(addressObj.recname) ||
                _.isEmpty(addressObj.phone) ||
                _.isEmpty(addressObj.province) ||
                _.isEmpty(addressObj.city) ||
                _.isEmpty(addressObj.town) ||
                _.isEmpty(addressObj.address)) {
                wineApp.tool.toastError('*为必填项，请输入');
                return;
            }
            //var ajaxUrl = wineApp.CONFIG.API.addressadd;
            wineApp.ajax.post(ajaxUrl, {
                address: addressObj
            }).done(function(data) {
                if (!_.isNil(referURL)) {
                    window.location.href = referURL;
                } else {
                    window.location.href = "address.html";
                }
            });
        });
        $('#province').on('change', function(event) {
            //var ajaxUrl = wineApp.CONFIG.API.citys
            var id = $('#province').val();
            loadCity(id);

        });
        $('#city').on('change', function(event) {
            //var ajaxUrl = wineApp.CONFIG.API.towns
            var id = $('#city').val();
            loadTown(id)
        });
    }


    return {
        initPage: initPage
    }
})()

$(document).ready(function() {
    $('.wine_footer').remove();
    wineApp.common.initHeaderIcon(0, true, {
        camera: false,
        search: false
    });
    wineApp.modaddress.initPage();
});